﻿using Prism.Ioc;
using System.Configuration;
using System.Collections.Generic;
using FrameWork.Core.Configuration;
using System.Runtime.CompilerServices;

namespace FrameWork.Core.Infrastructure
{
    public class EngineContext
    {
        #region __方法__
        [MethodImpl(MethodImplOptions.Synchronized)]
        public static IEngine Initialize(bool forceRecreate, IContainerExtension container = null)
        {
            if (Singleton<IEngine>.Instance == null || forceRecreate)
            {
                Singleton<IEngine>.Instance = new InvengoEngine();
                var config = ConfigurationManager.GetSection("InvengoConfig") as InvengoConfig;
                Singleton<IEngine>.Instance.Initialize(config ?? new InvengoConfig(), container);
            }
            return Singleton<IEngine>.Instance;
        }

        public static void Replace(IEngine engine)
        {
            Singleton<IEngine>.Instance = engine;
        }

        #endregion

        #region __属性__
        public static IEngine Current
        {
            get
            {
                if (Singleton<IEngine>.Instance == null)
                {
                    Initialize(false);
                }
                return Singleton<IEngine>.Instance;
            }
        }
        /// <summary>
        ///AppDomainTypeFinder.LoadMatchingAssemblies加载模块时的条件
        ///1、为空时 根据默认规则加载模块
        ///2、不为空时，只加载_loadModules集合中匹配的模块
        ///解决-FrameWork.DataStorage启动时非必要的模块导致的报错问题
        /// </summary>
        private static List<string> _loadModules;
        public static List<string> LoadModules
        {
            get 
            {
                if (_loadModules==null) 
                {
                    _loadModules=new List<string>();
                }
                return _loadModules;
            }
        }
        #endregion
    }
}
